package org.gordonframework.site.forms.elements.skins {
	import org.gordonframework.core.graphics.GDraw;
	import org.gordonframework.site.forms.elements.GCheckBox;
	import org.gordonframework.site.forms.elements.GElement;
	import org.gordonframework.site.forms.elements.skins.GElementSkin;
	import org.gordonframework.site.views.GLabel;
	import org.gordonframework.site.views.GView;
	
	import flash.events.MouseEvent;	

	/**
	 * @author eriarv
	 */
	public class GCheckBoxSkin extends GElementSkin {

		protected var boxOn : GView;		protected var boxOff : GView;
		
		private var _isOn : Boolean;

		public function GCheckBoxSkin() {
			super();
			
			// Checked state
			boxOn = new GView();
			boxOff = new GView();
			
			boxOn.buttonMode = true;
			boxOff.buttonMode = true;
//			boxOn.addEventListener(MouseEvent.CLICK, mouseClickHandler);
//			boxOff.addEventListener(MouseEvent.CLICK, mouseClickHandler);
			
			boxOn.buttonMode = true;
			boxOff.buttonMode = true;
			hitArea = boxOn;
			
			label = new GLabel();
			label.wordWrap = false;

			addChild(boxOn);
			addChild(boxOff);
			addChild(label);
		}
		
		private function mouseClickHandler(event : MouseEvent) : void {
		}

		override public function update(element : GElement) : void {
			super.update(element);
			
			boxOn.graphics.lineStyle(format.boxOn.border.thickness, format.boxOn.border.color, format.boxOn.border.alpha);
			boxOn.graphics.beginFill(format.boxOn.background.color);
			GDraw.rect(boxOn.graphics, format.boxOn.background.width, format.boxOn.background.height );
			boxOn.graphics.endFill();
			
			// marker
			boxOn.graphics.lineStyle(format.boxOn.marker.thickness, format.boxOn.marker.color, format.boxOn.marker.alpha);
			boxOn.graphics.moveTo(3, 5);
			boxOn.graphics.lineTo(5, 8);
			boxOn.graphics.lineTo(9, 3);
			
			boxOff.graphics.lineStyle(format.boxOff.border.thickness, format.boxOff.border.color, format.boxOff.border.alpha);
			boxOff.graphics.beginFill(format.boxOff.background.color);
			GDraw.rect(boxOff.graphics, format.boxOff.background.width, format.boxOff.background.height );
			boxOff.graphics.endFill();
			
			label.setDefaultTextFormat(textFormats.label);
			label.text = element.text;
			label.x = boxOn.right + format.paddingX;
			label.y = format.paddingY;
			
			isOn = (element as GCheckBox).isOn;
		}
		
		public function get isOn() : Boolean {
			return _isOn;
		}
		
		public function set isOn(isOn : Boolean) : void {
			_isOn = isOn;
			boxOn.visible = isOn;
			boxOff.visible = !isOn;
		}
	}
}
